from uiautomation import WindowControl
import pyautogui as pyau


class wxSession(object):
    def __init__(self) -> None:
        self.wxWindow: object = WindowControl(Name='微信')
        self.session: object = self.wxWindow.ListControl(Name='会话')


    def Session(self, keyword: dict = None) -> None:
        msg_kw: str = '新消息'
        start_bool: bool = True
        self.wxWindow.SwitchToThisWindow()
        while start_bool:
            unread_list: list = []
            sessWin: object = self.session.GetChildren()
            for sess_msg in sessWin:
                if msg_kw in sess_msg.Name:
                    unread_list.append(sess_msg)
            for sess_msg in unread_list:
                unread_msg: str = sess_msg.GetFirstChildControl().GetChildren()[1].GetLastChildControl().GetFirstChildControl().Name
                if unread_msg in keyword:
                    sess_msg.Click()
                    self.wxWindow.SendKeys(keyword[unread_msg])
                    self.wxWindow.SendKeys('{Enter}')
                    pyau.moveTo(pyau.size()[0] / 2, pyau.size()[1] / 2)


if __name__ == '__main__':
    keyword: dict = {'你好': '恁好', 'test': 'test成功', 'e': '！！！'}
    wx: wxSession = wxSession()
    wx.Session(keyword)